package com.caigou.component;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class RequestComponent {

    public static String HttpGet(String url){
        //单位毫秒
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectionRequestTimeout(3000).setConnectTimeout(3000)
                .setSocketTimeout(3000).build();//设置请求的状态参数

        CloseableHttpClient httpclient = HttpClients.createDefault();//创建 CloseableHttpClient
        HttpGet httpGet = new HttpGet(url);
        httpGet.setConfig(requestConfig);

        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httpGet);//返回请求执行结果
            int statusCode = response.getStatusLine().getStatusCode();//获取返回的状态值
            if (statusCode != HttpStatus.SC_OK) {
                return null;
            } else {
                return EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            log.error("httpGet Exception handle-- > " + e);
        } finally {
            if (response != null){
                try {
                    response.close();//关闭response
                } catch (IOException e) {
                    log.error("httpGet IOException handle-- > " + e);
                }
            }
            if(httpclient != null){
                try {
                    httpclient.close();//关闭httpclient
                } catch (IOException e) {
                    log.error("httpGet IOException handle-- > " + e);
                }
            }
        }
        return null;
    }

    public static String HttpPost(String url, String jsonString){
        CloseableHttpResponse response = null;
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();//创建CloseableHttpClient
        HttpPost httpPost = new HttpPost(url);//实现HttpPost
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3000).setConnectTimeout(3000).build();
        httpPost.setConfig(requestConfig); //设置httpPost的状态参数
        httpPost.addHeader("Content-Type", "application/json");//设置httpPost的请求头中的MIME类型为json
        StringEntity requestEntity = new StringEntity(jsonString, "utf-8");
        httpPost.setEntity(requestEntity);//设置请求体
        try {
            response = httpClient.execute(httpPost, new BasicHttpContext());//执行请求返回结果
            if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                return null;
            }
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                return EntityUtils.toString(entity, "utf-8");
            } else {
                return null;
            }
        } catch (Exception e) {
            log.error("httpPost method exception handle-- > " + e);
            return null;
        } finally {
            if (response != null){
                try {
                    response.close();//最后关闭response
                } catch (IOException e) {
                    log.error("httpPost method IOException handle -- > " + e);
                }
            }if(httpClient != null){
                try {
                    httpClient.close();
                } catch (IOException e) {
                    log.error("httpPost method exception handle-- > " + e);
                }
            }
        }
    }
}
